package com.morningstar.keyratio;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;


public class KeyRatioEntity{
	
	private String title;
	private List<Section> sections;
	
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<Section> getSections() {
		return sections;
	}

	public void setSections(List<Section> sections) {
		this.sections = sections;
	}

	public KeyRatioEntity(String dataStr){
		if(dataStr.trim().equals("")){//some stocks don't have keyratio data
			setTitle("Stock data is not existed!");
			setSections(new ArrayList<>());
		}
		else{
			String[] lines = dataStr.split("\n");
			
			setTitle(lines[0]);
			
			List<Integer> lineNums = new ArrayList<>();
			for(int i=0;i<Constants.SECTION_MARKS.length;i++){
				String mark = Constants.SECTION_MARKS[i];
				
				for(int j=1;j<lines.length;j++){
					if(lines[j].contains(mark)){
						lineNums.add(j);
					}
				}
			}
			
			
			List<Section> sections = new ArrayList<>();
			for(int i=0;i<lineNums.size()-1;i++){
				sections.add(new Section(Arrays.copyOfRange(lines, lineNums.get(i),lineNums.get(i+1))));
			}
			
			sections.add(new Section(Arrays.copyOfRange(lines, lineNums.get(lineNums.size()-1),lines.length)));
			
			setSections(sections);
		}
	}
	
	
	
	public static class Section{
		private String title;
		private List<SubSection> subSections;
		
		public Section(String[] sectionLines){
			//System.out.println(sectionLines[0]);
			//System.out.println("-----------------");
			
			setTitle(sectionLines[0]);
			
			List<SubSection> subSections = new ArrayList<>();
			int i,lastIdx;
			for(i=1,lastIdx=1;i<sectionLines.length;i++){
				if(sectionLines[i].trim().equals("")){
					subSections.add(new SubSection(Arrays.copyOfRange(sectionLines, lastIdx, i)));
					lastIdx = i;
				}
			}
			if(i==sectionLines.length && lastIdx==1){
				subSections.add(new SubSection(Arrays.copyOfRange(sectionLines,1,sectionLines.length)));
			}
			
			setSubSections(subSections);
		}
		
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public List<SubSection> getSubSections() {
			return subSections;
		}
		public void setSubSections(List<SubSection> subSections) {
			this.subSections = subSections;
		}
		
		
		
	}
	
	public static class SubSection{
		private String title;
		private List<String> years;
		private List<KeyIndex> indices;
		
		public SubSection(String[] data){
			//String[] data = line.split(Constants.SEP);
			//System.out.println(data[0]);
			//System.out.println(data[data.length-1]);
			//System.out.println("\r\n");
			
			List<String> cleanData = new ArrayList<>();
			for(String d:data){
				if(!d.trim().equals("")){
					cleanData.add(d);
				}
			}
			
			data = cleanData.toArray(new String[0]);
			
			String[] firstLine = data[0].split(Constants.SEP);
			this.setTitle(firstLine[0]);
			this.setYears(Arrays.asList(Arrays.copyOfRange(firstLine, 1,firstLine.length)));
			
			List<KeyIndex> indexs = new ArrayList<>();
			for(int i=1;i<data.length;i++){
				if(!data[i].trim().equals("")){
					indexs.add(new KeyIndex(data[i]));
				}
			}
			setIndices(indexs);
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public List<String> getYears() {
			return years;
		}

		public void setYears(List<String> years) {
			this.years = years;
		}

		public List<KeyIndex> getIndices() {
			return indices;
		}

		public void setIndices(List<KeyIndex> indices) {
			this.indices = indices;
		}
		
		
	}
	
	public static class KeyIndex{
		private String title;
		private List<Float> data;
		
		public KeyIndex(String line){
			String[] data = line.split(Constants.SEP);
			
			this.setTitle(data[0]);
			
			String[] realData = Arrays.copyOfRange(data, 1,data.length);
			Float[] realDataFloat = new Float[realData.length];
			for(int i=0;i<realData.length;i++){
				if(realData[i] != null && !"".equals(realData[i])){
					realDataFloat[i] = Float.valueOf(realData[i].replace("\"", ""));
				}
				else{
					realDataFloat[i] = 0f;
				}
			}
			
			this.setData(Arrays.asList(realDataFloat));
			
		}
		
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public List<Float> getData() {
			return data;
		}
		public void setData(List<Float> data) {
			this.data = data;
		}
		
		
		
	}
	

}
